package br.com.ambgis.sicap;

import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class GerenciadorPlacaActivity extends ListActivity {

	PlacaDao dao;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gerenciador_placa);
		setupActionBar();
		
		DbHelper helper = new DbHelper(this);
		dao = new PlacaDao(helper.getWritableDatabase());
		List<Placa> all = dao.getAllForListing();
		final ArrayAdapter<Placa> adapter = new PlacaArrayAdapter(this, all);
		setListAdapter(adapter);
		ListView list = getListView();
		list.setOnItemLongClickListener(new OnItemLongClickListener() {
		      @Override
		      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		    	  
		    	  final Placa model = (Placa) parent.getItemAtPosition(position);
		    	  
		    	  final AlertDialog.Builder builder = new AlertDialog.Builder(GerenciadorPlacaActivity.this);
		    	  builder.setMessage(R.string.signDeletionDialogText)
		    	  		.setTitle(R.string.delete)
						.setCancelable(true)
						.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface d, int id) {
								dao.delete(model.getId());
								adapter.remove(model);
								d.dismiss();
							}
						})
						.setNegativeButton(R.string.no,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface d, int id) {
										d.cancel();
									}
								});
		    	  builder.create().show();
		    	  
		    	  return true;
		      }
		});
	}		

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		dao.close();
		super.onDestroy();
	}

}
